﻿<%@page import="cn.bdqn.pojo.NewsCategory"%>
<%@page import="cn.bdqn.util.PageSupport"%>
<%@page import="com.mysql.jdbc.StringUtils"%>
<%@page import="cn.bdqn.pojo.News"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../../common/common.jsp" %> 
 <script type="text/javascript">
	function addNews(){
		window.location="newsDetailCreate.jsp";
	}
	function page_nav(frm,num){
		frm.pageIndex.value = num;
		frm.submit();
	}
	function jump_to(frm,pageno){
		//验证用户的输入
		var regexp=/^[1-9]\d*$/;
		var totalPageCount = document.getElementById("totalPageCount").value;
		if(!regexp.test(pageno)){
			alert("请输入正确的数字！");
			return false;
		}else if((pageno-totalPageCount) > 0){
			alert("总页码一共"+totalPageCount+"页，请输入正确的页码！");
			return false;
		}else{
			page_nav(frm,pageno);
		}
	}
</script>
  <%
  	  request.setCharacterEncoding("utf-8"); 
	  String pageIndex = request.getParameter("pageIndex");
	  String title = request.getParameter("title");
	  if(StringUtils.isNullOrEmpty(title)){
			title = "";
	  }
	  String categoryId = request.getParameter("categoryId");
	  if(StringUtils.isNullOrEmpty(categoryId)){
			categoryId = "-1";
	  }
	  request.setAttribute("queryTitle", title);
	  request.setAttribute("querycategoryId", categoryId);
	   
   %>
  <div class="main-content-right">
        <!--即时新闻-->
        <div class="main-text-box">
            <div class="main-text-box-tbg">
                <div class="main-text-box-bbg">
                    <form name ="searchForm" id="searchForm" action="/news/jsp/admin/newsDetailList.jsp" method="post">
		 	<div>
		 				新闻分类：
							<select name="categoryId">
							    <option value="-1">--请选择--</option>
								<%--  <%
								   List<NewsCategory> newsCategoryList=newsCategoryService.getNewsCategoryList(); 
								   for(NewsCategory newsCategory:newsCategoryList){%>
								   	<option value="<%=newsCategory.getId() %>"><%=newsCategory.getName() %></option>
								 <%} %> --%>
								 
								 <%
								 List<NewsCategory> newsCategoryList=newsCategoryService.getNewsCategoryList(); 
								 for(NewsCategory newsCategory:newsCategoryList){%>
								   	<option 
								   		<%if(newsCategory.getId() == Integer.parseInt((String)request.getAttribute("querycategoryId"))){%> 
								   				selected="selected" <%} %>
								   		value="<%=newsCategory.getId() %>"><%=newsCategory.getName() %>
								   	</option>
								 <%} %>
						</select>
		 				新闻标题<input type="text" name="title" id="title" value="<%=(String)request.getAttribute("queryTitle")%>"/>
		 					<button type="submit" class="page-btn">查询</button>
		 					<button type="button" onclick="addNews();" class="page-btn">增加</button>
		 					<input type="hidden" name="pageIndex" value="1"/>
		 	</div>
		 	</form>
			<table cellpadding="1" cellspacing="1" class="admin-list">
				<thead >
					<tr class="admin-list-head">
						<th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <%	
                	//设置页面容量
                	int pageSize = 2;
             
                	//当前页码
                	int currentPageNo = 1;
                	//String pageIndex = request.getParameter("pageIndex");
	  				//String title = request.getParameter("title");
	  				//String categoryId = request.getParameter("categoryId");
                	News _news = new News();
                	System.out.println("query pageIndex--------- > " + pageIndex);
                	System.out.println("query title --------- > " + title);
                	System.out.println("query categoryId --------- > " + categoryId);
                	
                	if(title != null){
                		_news.setTitle(title);
                	}
                	if(categoryId != null){
                		_news.setCategoryId(Integer.valueOf(categoryId));
                	}
                	if(pageIndex != null){
                		try{
                			currentPageNo = Integer.valueOf(pageIndex);
                		}catch(NumberFormatException e){
                			response.sendRedirect("error.jsp");
                		}
                	}	
                	//总数量（表）	
                	int totalCount	= newsService.getNewsCount(_news);
                	
                	//总页数
                	PageSupport pages=new PageSupport();
                	pages.setCurrentPageNo(currentPageNo);
                	pages.setPageSize(pageSize);
                	pages.setTotalCount(totalCount);
                	
                	int totalPageCount = pages.getTotalPageCount();
                	
                	//控制首页和尾页
                	if(currentPageNo < 1){
                		currentPageNo = 1;
                	}else if(currentPageNo > totalPageCount){
                		currentPageNo = totalPageCount;
                	}
                	
                	//每页显示的新闻列表
                	//List<News> newsList=newsService.getNewsList();
                	List<News> newsList=newsService.getPageNewsList(currentPageNo, pageSize, _news);
                	int i=0;
                	for(News news:newsList){
                		i++;
                %>
                <input type="hidden" name="totalPageCount" id="totalPageCount" value="<%=totalPageCount %>"/>
                <tbody>
                	<tr <%if(i%2 == 0){%>class="admin-list-td-h2"<%} %>>
                		<td><a href='adminNewsView.jsp?id=<%=news.getId()%>'><%=news.getTitle() %></a></td>
                		<td><%=news.getAuthor()%></td>
                		<td><%=news.getCreateDate() %></td>
                		<td><a href='adminNewsEdit.jsp?id=<%=news.getId()%>'>修改</a>
                			<a href="javascript:if(confirm('确认是否删除此新闻？')) location='adminNewsDel.jsp?id=2'">删除</a>
                		</td>
                	</tr> 
                </tbody>
                <%
                	}
                %>
            </table>
           <div class="page-bar">
			<ul class="page-num-ul clearfix">
				<li>共<%=totalCount %>条记录&nbsp;&nbsp; <%=currentPageNo %>/<%=totalPageCount %>页</li>
				<%
				 if(currentPageNo > 1){
				 %>
					<a href="javascript:page_nav(document.forms[0],1);">首页</a>
					<a href="javascript:page_nav(document.forms[0],<%=currentPageNo-1%>);">上一页</a>
				<%}if(currentPageNo < totalPageCount) {%>
					<a href="javascript:page_nav(document.forms[0],<%=currentPageNo+1%>);">下一页</a>
					<a href="javascript:page_nav(document.forms[0],<%=totalPageCount%>);">尾页</a>
				<%} %>
				&nbsp;&nbsp;
			</ul>
		 <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key" />页
	     <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
		</span>
		</div> 
        </div>
       </div>
   </div>
   </div>
</div>